﻿using Kimd.Common;
using Kimd.Communication;
using Kimd.Logic;
using Kimd.Machine;
using Kimd.Motion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkStation.Common;

namespace WorkStation.Logic
{
    public class RobotStatusLogic2 : FlowBase
    {
        private bool[] _robotInStatus2 = new bool[56];//7*8=56
        private bool[] writeStatus2 = new bool[56];
        private TCPClient _robotClient2;
        private Pdo _robotReadAddress7;
        private Pdo _robotReadAddress8;
        private Pdo _robotReadAddress9;
        private Pdo _robotReadAddress10;
        private Pdo _robotReadAddress11;
        private Pdo _robotReadAddress12;
        private Pdo _robotReadAddress13;
        public Dictionary<int, string> RobotInEnumRobot2Dict;
        public Dictionary<int, string> RobotOutEnumRobot2Dict;
        public RobotStatusLogic2(string flowName, FlowManager flowmanager) : base(flowName, flowmanager)
        {
            if (MachineResource.Instance.MachineTcpClient.ContainsKey("机械手状态2"))
            {
                _robotClient2 = MachineResource.Instance.MachineTcpClient["机械手状态2"];
            }
            _robotReadAddress7 = new Pdo(MachineResource.Instance.MachineCard["card1"].Description, 2, 16, "机器人读地址1", "Robot2模组");
            _robotReadAddress7.Card = MachineResource.Instance.MachineCard["card1"];
            _robotReadAddress8 = new Pdo(MachineResource.Instance.MachineCard["card1"].Description, 2, 17, "机器人读地址2", "Robot2模组");
            _robotReadAddress8.Card = MachineResource.Instance.MachineCard["card1"];
            _robotReadAddress9 = new Pdo(MachineResource.Instance.MachineCard["card1"].Description, 2, 18, "机器人读地址3", "Robot2模组");
            _robotReadAddress9.Card = MachineResource.Instance.MachineCard["card1"];
            _robotReadAddress10 = new Pdo(MachineResource.Instance.MachineCard["card1"].Description, 2, 19, "机器人读地址4", "Robot2模组");
            _robotReadAddress10.Card = MachineResource.Instance.MachineCard["card1"];
            _robotReadAddress11 = new Pdo(MachineResource.Instance.MachineCard["card1"].Description, 2, 20, "机器人读地址5", "Robot2模组");
            _robotReadAddress11.Card = MachineResource.Instance.MachineCard["card1"];
            _robotReadAddress12 = new Pdo(MachineResource.Instance.MachineCard["card1"].Description, 2, 21, "机器人读地址6", "Robot2模组");
            _robotReadAddress12.Card = MachineResource.Instance.MachineCard["card1"];
            _robotReadAddress13 = new Pdo(MachineResource.Instance.MachineCard["card1"].Description, 2, 22, "机器人读地址7", "Robot2模组");
            _robotReadAddress13.Card = MachineResource.Instance.MachineCard["card1"];

            RobotInEnumRobot2Dict = new Dictionary<int, string>();
            foreach (var item in Enum.GetValues(typeof(RobotinEnumRobot2)))
            {
                RobotInEnumRobot2Dict.Add((int)item, EnumHelper.GetEnumDescriptionFromValue((RobotinEnumRobot2)item));
            }

            RobotOutEnumRobot2Dict = new Dictionary<int, string>();
            foreach (var item in Enum.GetValues(typeof(RobotoutEnumRobot2)))
            {
                RobotOutEnumRobot2Dict.Add((int)item, EnumHelper.GetEnumDescriptionFromValue((RobotoutEnumRobot2)item));
            }
        }

        public override void Calibrate()
        {
            base.Calibrate();
        }

        public override void Home()
        {
            base.Home();
        }

        public override void Init(out string errInfo)
        {
            base.Init(out errInfo);
        }

        public override void Method()
        {
            if (!GlobalVariable.RobotCommDisable)
            {
                int val7 = _robotReadAddress7.ReadData();
                int val8 = _robotReadAddress8.ReadData();
                int val9 = _robotReadAddress9.ReadData();
                int val10 = _robotReadAddress10.ReadData();
                int val11 = _robotReadAddress11.ReadData();
                int val12 = _robotReadAddress12.ReadData();
                int val13 = _robotReadAddress13.ReadData();

                for (int i = 0; i < 8; i++)
                {
                    _robotInStatus2[i] = ((val7 >> i) & 0x01) == 1;
                }
                for (int i = 0; i < 8; i++)
                {
                    _robotInStatus2[i + 8] = ((val8 >> i) & 0x01) == 1;
                }
                for (int i = 0; i < 8; i++)
                {
                    _robotInStatus2[i + 16] = ((val9 >> i) & 0x01) == 1;
                }
                for (int i = 0; i < 8; i++)
                {
                    _robotInStatus2[i + 24] = ((val10 >> i) & 0x01) == 1;
                }
                for (int i = 0; i < 8; i++)
                {
                    _robotInStatus2[i + 32] = ((val11 >> i) & 0x01) == 1;
                }
                for (int i = 0; i < 8; i++)
                {
                    _robotInStatus2[i + 40] = ((val12 >> i) & 0x01) == 1;
                }
                for (int i = 0; i < 8; i++)
                {
                    _robotInStatus2[i + 48] = ((val13 >> i) & 0x01) == 1;
                }
                foreach (var item in Enum.GetValues(typeof(RobotinEnumRobot2)))
                {
                    if (((int)item >= 16 && (int)item <= 39))
                    {
                        if (_robotInStatus2[(int)item])
                        {
                            MachineResource.Instance.MachineCylinder[RobotInEnumRobot2Dict[(int)item]].On();
                        }
                        else
                        {
                            MachineResource.Instance.MachineCylinder[RobotInEnumRobot2Dict[(int)item]].Off();
                        }
                    }
                    else if ((int)item <= 15 || (int)item == 40)
                    {
                        if ((int)item == 40)
                        {

                        }
                        GlobalVariable.RobotStatus2[(int)item] = _robotInStatus2[(int)item];
                    }
                    else
                    {
                        if ((int)item == 41)
                            GlobalVariable.NeedOpenPrecisionFeedProductVacUp[1] = _robotInStatus2[(int)item];
                        else if ((int)item == 42)
                            GlobalVariable.NeedClosePrecisionRecycleProductVacUp[1] = _robotInStatus2[(int)item];
                        else if ((int)item == 43)
                            GlobalVariable.NeedOpenPrecisionFeedProductVacDown[1] = _robotInStatus2[(int)item];
                        else if ((int)item == 44)
                            GlobalVariable.NeedClosePrecisionRecycleProductVacDown[1] = _robotInStatus2[(int)item];
                        else if ((int)item == 45)
                            GlobalVariable.RobotZInNotSafeArea[1] = _robotInStatus2[(int)item];
                        else if ((int)item == 46)
                            GlobalVariable.NozzleAlreadyLiftUp[1] = _robotInStatus2[(int)item];
                        else if ((int)item == 47)
                            GlobalVariable.NozzleAlreadyLiftDown[1] = _robotInStatus2[(int)item];
                        else if ((int)item == 48)
                            GlobalVariable.RobotCylinderWarn[1] = _robotInStatus2[(int)item];
                    }
            }

                foreach (var item in Enum.GetValues(typeof(RobotoutEnumRobot2)))
                {
                    if ((int)item >= 16 && (int)item < 40)
                    {
                        writeStatus2[(int)item] = MachineResource.Instance.MachineSensor[RobotOutEnumRobot2Dict[(int)item]].WaitOn();
                    }
                    else if ((int)item < 16)
                    {
                        writeStatus2[(int)item] = GlobalVariable.RobotControlStatus2[(int)item];
                    }
                    else if ((int)item >= 40 && (int)item <= 43)
                    {
                        if ((int)item == 40)
                            writeStatus2[(int)item] = GlobalVariable.AlreadyOpenPrecisionFeedProductVacUp[1];
                        else if ((int)item == 41)
                            writeStatus2[(int)item] = GlobalVariable.AlreadyClosePrecisionRecycleProductVacUp[1];
                        if ((int)item == 42)
                            writeStatus2[(int)item] = GlobalVariable.AlreadyOpenPrecisionFeedProductVacDown[1];
                        else if ((int)item == 43)
                            writeStatus2[(int)item] = GlobalVariable.AlreadyClosePrecisionRecycleProductVacDown[1];
                    }
                    else if ((int)item == 44)
                    {
                        writeStatus2[(int)item] = GlobalVariable.IsRobotDryRun;
                    }
                    else if ((int)item == 45)
                    {
                        writeStatus2[(int)item] = GlobalVariable.AlreadyGrabProductUp[1];
                    }
                    else if ((int)item == 46)
                    {
                        writeStatus2[(int)item] = GlobalVariable.AlreadyGrabProductDown[1];
                    }
                    else if ((int)item == 47)
                    {
                        writeStatus2[(int)item] = GlobalVariable.AlreadyRobotCylinderWarn[1];
                    }
                }

                int writeVal1 = 0;
                int writeVal2 = 0;
                int writeVal3 = 0;
                int writeVal4 = 0;
                int writeVal5 = 0;
                int writeVal6 = 0;
                int writeVal7 = 0;
                for (int w = 0; w < 8; w++)
                {
                    writeVal1 = setIntegerSomeBit(w, writeVal1, writeStatus2[w]);
                }
                for (int w = 0; w < 8; w++)
                {
                    writeVal2 = setIntegerSomeBit(w, writeVal2, writeStatus2[w + 8]);
                }
                for (int w = 0; w < 8; w++)
                {
                    writeVal3 = setIntegerSomeBit(w, writeVal3, writeStatus2[w + 16]);
                }
                for (int w = 0; w < 8; w++)
                {
                    writeVal4 = setIntegerSomeBit(w, writeVal4, writeStatus2[w + 24]);
                }
                for (int w = 0; w < 8; w++)
                {
                    writeVal5 = setIntegerSomeBit(w, writeVal5, writeStatus2[w + 32]);
                }
                for (int w = 0; w < 8; w++)
                {
                    writeVal6 = setIntegerSomeBit(w, writeVal6, writeStatus2[w + 40]);
                }
                for (int w = 0; w < 8; w++)
                {
                    writeVal7 = setIntegerSomeBit(w, writeVal7, writeStatus2[w + 48]);
                }
                _robotReadAddress7.SetData(writeVal1);
                _robotReadAddress8.SetData(writeVal2);
                _robotReadAddress9.SetData(writeVal3);
                _robotReadAddress10.SetData(writeVal4);
                _robotReadAddress11.SetData(writeVal5);
                _robotReadAddress12.SetData(writeVal6);
                _robotReadAddress13.SetData(writeVal7);
                //GC.Collect();
            }
        }

        public override void OnStop()
        {
            base.OnStop();
        }

        public override bool PreStartCheck()
        {
            return base.PreStartCheck();
        }
        private int setIntegerSomeBit(int mask, int a, bool flag)
        {
            if (flag)
            {
                a |= (0x1 << mask);
            }
            else
            {
                a &= ~(0x1 << mask);
            }
            return a;
        }
    }
}
